package com.epam.iunna.bookshop.model.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
//import java.sql.SQLException;
//
//import com.epam.iunna.bookshop.model.User;
//import com.epam.iunna.bookshop.model.UserDao;

/**
 * @author Iunna
 *
 */
public class HashGenerator {
    
    public static synchronized String generateHash(String pass){
        try
        {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            byte[] byteHash = messageDigest.digest(pass.getBytes());
            BigInteger bigIntegerHash = new BigInteger(1,byteHash);
            return bigIntegerHash.toString(16);
        }
        catch(NoSuchAlgorithmException e)
        {
            throw new RuntimeException(e);
        }       
    }
    
//    /**
//     * @param byteArray
//     * @return
//     */
//    private static String byteArrayToIntString(byte[] byteArray){
//        StringBuilder intString = new StringBuilder("");
//        for(Byte currentByte: byteArray){
//            intString.append(currentByte.shortValue());
//        }
//        System.out.println(intString.toString());
//        return intString.toString();
//    }
    
//    /**
//     * Change passwords of all users to its hash
//     * @param args
//     * @throws SQLException 
//     */
//    public static void main(String[] args) throws SQLException {
//        UserDao userDao = new UserDao();
//        for(User user: userDao.getAll()){
//            userDao.setPassword(user, generateHash(user.getPass()));
//        }
//    }

}
